import {listRecordByPageUsingPOST} from '@/services/api/fileRecordController';
import type {ActionType, ProColumns} from '@ant-design/pro-table';
import {
  PageContainer,
  ProTable,
} from '@ant-design/pro-components';
import {useRef} from 'react';
import {getLoginUserUsingGET} from "@/services/api/userController";
import {history} from "@@/core/history";


export const waitTimePromise = async (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

export const waitTime = async (time: number = 100) => {
  await waitTimePromise(time);
};

const columns: ProColumns<API.FileRecord>[] = [
  {
    dataIndex: 'id',
    valueType: 'indexBorder',
    width: 48,
  },
  {
    title: '输入文件路径',
    dataIndex: 'inputPath',
    valueType: 'textarea',
    ellipsis: true,
    copyable: true,
    hideInSearch: true,
  },
  {
    disable: true,
    title: '操作类型',
    dataIndex: 'doType',
    valueType: 'select',
    filters: true,
    onFilter: true,
    ellipsis: true,
    valueEnum: {
      json2csv: {text: 'json2csv', status: 'Success'},
      json2xml: {
        text: 'json2xml',
        status: 'Success',
      },
      json2yaml: {
        text: 'json2yaml',
        status: 'Success',
      },
      jsonFormat: {text: 'jsonFormat', status: 'Success'},
      jsonValid: {
        text: 'jsonValid',
        status: 'Success',
      },
      jsonCompress: {
        text: 'jsonCompress',
        status: 'Success',
      },
      csv2json: {text: 'csv2json', status: 'Success'},
      csv2xml: {
        text: 'csv2xml',
        status: 'Default',
      },
      csv2yaml: {
        text: 'csv2yaml',
        status: 'Default',
      },
      csvFormat: {text: 'csvFormat', status: 'Default'},
      csvValid: {
        text: 'csvValid',
        status: 'Default',
      },
      xml2json: {text: 'xml2json', status: 'Processing'},
      xml2csv: {
        text: 'xml2csv',
        status: 'Processing',
      },
      xml2yaml: {
        text: 'xml2yaml',
        status: 'Processing',
      },
      xmlFormat: {text: 'xmlFormat', status: 'Processing'},
      xmlValid: {
        text: 'xmlValid',
        status: 'Processing',
      },
      xmlCompress: {
        text: 'xmlCompress',
        status: 'Processing',
      },
      yaml2json: {text: 'yaml2json', status: 'Error'},
      yaml2csv: {
        text: 'yaml2csv',
        status: 'Error',
      },
      yaml2xml: {
        text: 'yaml2xml',
        status: 'Error',
      },
      yamlFormat: {text: 'yamlFormat', status: 'Error'},
      yamlValid: {
        text: 'yamlValid',
        status: 'Error',
      },
    },
  },
  {
    title: '输出文件路径',
    dataIndex: 'outputPath',
    valueType: 'textarea',
    ellipsis: true,
    copyable: true,
    hideInSearch: true,
  },
  {
    title: '操作时间',
    dataIndex: 'createTime',
    valueType: 'dateTime',
    hideInSearch: true,
    sorter: true,
  },

  {
    disable: true,
    title: '状态',
    dataIndex: 'recordStatus',
    filters: true,
    onFilter: true,
    ellipsis: true,
    valueType: 'select',
    valueEnum: {
      0: {text: '操作成功', status: 'Success'},
      1: {
        text: '已删除',
        status: 'Error',
      },
      2: {
        text: '异常',
        status: 'Processing',
      },
    },
  },
];

const fetchUserInfo = async () => {
  try {
    const res = await getLoginUserUsingGET();
    return res.data;
  } catch (error) {
    history.push('/user/login');
  }
  return undefined;
};

export default () => {
  const actionRef = useRef<ActionType>();
  const user = fetchUserInfo();
  return (
    <PageContainer>
      <div style={{maxHeight: 'calc(100vh - 220px)', minHeight: 'calc(100vh - 200px)', overflow: 'auto'}}>
        <ProTable<API.FileRecord>
          columns={columns}
          actionRef={actionRef}
          cardBordered
          request={async (params, sort, filter) => {
            const res: any = await listRecordByPageUsingPOST(
              {
                ...user,
                ...params,
              }
            );
            await waitTime(2000);
            if (res?.data) {
              return {
                data: res?.data.records || [],
                success: true,
                total: res?.data.total || 0,
              };
            } else {
              return {
                data: [],
                success: false,
                total: 0,
              };
            }
          }}
          editable={{
            type: 'multiple',
          }}
          columnsState={{
            persistenceKey: 'pro-table-singe-demos',
            persistenceType: 'localStorage',
          }}
          rowKey="id"
          //开启行选择
          rowSelection={{}}
          search={{
            labelWidth: 'auto',
          }}
          options={{
            setting: {
              listsHeight: 400,
            },
          }}
          pagination={{
            pageSize: 10,
          }}
          dateFormatter="string"
          headerTitle="服务记录"
        />
      </div>
    </PageContainer>
  );
};
